using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DAL;
using DTO;
using BUS;
using Utility;

public partial class Admin_SanPham_Add : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        LoadDropDownList();
        LoadData();
    }

    private void LoadDropDownList()
    {
        tbDanhMucSanPhamCollection  dmCollection = tbDanhMucSanPhamBUS.GetAll();
        ddl_dm.DataTextField = tbDanhMucSanPhamColumns .tendanhmuc .ToString();
        ddl_dm.DataValueField = tbDanhMucSanPhamColumns .madanhmuc.ToString();
        ddl_dm.DataSource = dmCollection;
        ddl_dm.DataBind();

        tbDoanhNghiepCollection dnCollection = tbDoanhNghiepBUS.GetAll();
        ddl_dn.DataTextField = tbDoanhNghiepColumns.tendn .ToString();
        ddl_dn.DataValueField = tbDoanhNghiepColumns .madn.ToString();
        ddl_dn.DataSource = dnCollection;
        ddl_dn.DataBind();

        tbTinhTrangCollection ttCollection = tbTinhTrangBUS.GetAll();
        ddl_tinhtrang.DataTextField = tbTinhTrangColumns.tentinhtrang .ToString();
        ddl_tinhtrang.DataValueField = tbTinhTrangColumns.matinhtrang.ToString();
        ddl_tinhtrang.DataSource = ttCollection;
        ddl_tinhtrang.DataBind();
    }

    private void LoadData()
    {
        try
        {
            int id = int.Parse(Request.QueryString["id"].ToString());
            txt_MaSP.Text = id.ToString();

            tbSanPham sp = tbSanPhamBUS.GetByID(id);
            txt_TenSP.Text = sp.tensp;
            ddl_dm.SelectedValue = sp.danhmuc .ToString();
            ddl_dn.SelectedValue = sp.doanhnghiep.ToString();
            ddl_tinhtrang.SelectedValue = sp.tinhtrang.ToString();
            txt_giagoc.Text = sp.giagoc.ToString();
            txt_giaban.Text = sp.giaban.ToString();
            txt_soluongban.Text = sp.soluongban.ToString();
            txt_soluongtoida.Text = sp.soluongtoida.ToString();
            txt_damua.Text = sp.damua.ToString ();
            txt_chietkhau.Text = sp.chietkhau.ToString() ;
            txt_ngaysudung.Text = sp.ngaysudung.ToString();
            txt_ngayhethan.Text = sp.ngayhethan.ToString();
            ftb_mota.Text = sp.mota;
            ftb_diemnoibat.Text = sp.diemnoibat;
            ftb_dieukien.Text = sp.dieukien;
            ftb_quydinh.Text = sp.quydinh;
            img_HinhMinhHoa.ImageUrl = "~/Photos/Thumb/" + sp.hinhchinh ;
            ftb_thongtinchitiet.Text = sp.thongtinchitiet;
        }
        catch
        {
            Response.Redirect("~/Admin/SanPham_List.aspx");
        }
    }

    protected void btn_Update_Click(object sender, EventArgs e)
    {
        try
        {
            int id = int.Parse(Request.QueryString["id"].ToString());
            tbSanPham sp = tbSanPhamBUS.GetByID(id);
            sp.masp =int.Parse(txt_MaSP.Text .ToString ());
            sp.tensp  = txt_TenSP.Text;
            sp.danhmuc = int.Parse(ddl_dm.SelectedValue);
            sp.doanhnghiep = int.Parse(ddl_dm.SelectedValue);
            sp.tinhtrang = int.Parse (ddl_tinhtrang .SelectedValue );
            sp.giaban = float.Parse(txt_giaban.Text);
            sp.giagoc = float.Parse (txt_giagoc .Text);
            sp.soluongban = float.Parse (txt_soluongban .Text );
            sp.soluongtoida = float.Parse (txt_soluongtoida .Text );
            sp.damua = int.Parse (txt_damua .Text );
            sp.chietkhau = float.Parse (txt_chietkhau.Text);
            sp.ngaysudung = DateTime .Parse (txt_ngaysudung.Text);
            sp.ngayhethan =DateTime .Parse (txt_ngayhethan .Text);
            sp.quydinh = ftb_quydinh .Text ;
            sp.mota = ftb_mota.Text ;
            sp.diemnoibat = ftb_diemnoibat.Text;
            sp.dieukien = ftb_dieukien.Text;
            sp.thongtinchitiet = ftb_thongtinchitiet.Text;
            if (fu_HinhMinhHoa.HasFile)
            {
                string filename = DateTime.Now.Ticks.ToString() + "_" + fu_HinhMinhHoa.FileName;
                fu_HinhMinhHoa.SaveAs(Server.MapPath("~/Photos/" + filename));
                
                System.Drawing.Image img = System.Drawing.Image.FromFile(
                    Server.MapPath("~/Photos/" + filename));

                ImageUtility.MakeThumbnailPhoto(
                    img, 100, 100, Server.MapPath("~/Photos/Thumb/"), filename);
                // xoa hinh cu
                if (sp.hinhchinh  != "no_images.jpg")
                {
                    System.IO.File.Delete(Server.MapPath("~/Photos/" + sp.hinhchinh ));
                    System.IO.File.Delete(Server.MapPath("~/Photos/Thumb/" + sp.hinhchinh ));
                }
               sp.hinhchinh  = filename;
            }
            tbSanPhamBUS.Update(sp);            
        }
        catch
        {
            Response.Redirect("~/Admin/SanPham_List.aspx");
        }

        Response.Redirect(Request.RawUrl);
    }
 }
